package com.zzyl.nursing.service.impl;

import java.util.List;

import cn.hutool.json.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.constant.Constants;
import com.zzyl.common.utils.DateUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingLevelMapper;
import com.zzyl.nursing.domain.NursingLevel;
import com.zzyl.nursing.service.INursingLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;

/**
 * 护理等级Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-22
 */
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    /**
     * 查询护理等级
     *
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id) {
        return getById(id);
    }

    /**
     * 查询护理等级列表
     *
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel) {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel) {
        // 记录创建时间
        nursingLevel.setCreateTime(DateUtils.getNowDate());
        // 执行插入操作
        int flag = nursingLevelMapper.insertNursingLevel(nursingLevel);
        // 删除缓存
        deleteCache();
        return flag;
    }

    /**
     * 修改护理等级
     *
     * @param nursingLevel 护理等级对象，包含要更新的护理等级信息
     * @return 更新操作影响的记录数，成功更新返回大于0的整数，失败返回0或负数
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel) {
        // 记录更新时间
        nursingLevel.setUpdateTime(DateUtils.getNowDate());
        // 执行护理等级更新操作
        int flag = nursingLevelMapper.updateNursingLevel(nursingLevel);
        // 清除相关缓存数据
        deleteCache();
        return flag;
    }


    /**
     * 批量删除护理等级
     *
     * @param ids 需要删除的护理等级主键数组
     * @return 删除成功的记录数
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids) {
        // 调用mapper执行批量删除操作
        int flag = nursingLevelMapper.deleteNursingLevelByIds(ids);
        // 删除相关缓存
        deleteCache();
        return flag;
    }


    /**
     * 删除护理等级信息
     *
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id) {
        // 删除数据
        int flag = nursingLevelMapper.deleteNursingLevelById(id);
        // 删除缓存
        deleteCache();
        return flag;
    }

    /**
     * 查询所有护理等级
     * @return 护理等级列表
     */
    @Override
    public List<NursingLevel> listAll() {
        //TODO 缓存到redis中的数据格式不便于查看，后期需要对其进行优化

        // 先从缓存中查询是否有数据
        List<NursingLevel> nursingLevels = (List<NursingLevel>) redisTemplate.opsForValue().get(CacheConstants.NURSING_LEVEL_ALL_KEY);
        if (ObjectUtils.isNotEmpty(nursingLevels)) {
            return nursingLevels;
        }

        // 构造查询条件，只查询状态为1（启用）的护理等级
        LambdaQueryWrapper<NursingLevel> nursingLevelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        nursingLevelLambdaQueryWrapper.eq(NursingLevel::getStatus, 1);
        List<NursingLevel> list = list(nursingLevelLambdaQueryWrapper);

        // 将查询结果存入缓存
        redisTemplate.opsForValue().set(CacheConstants.NURSING_LEVEL_ALL_KEY, list);

        return list;
    }

    /**
     * 删除缓存
     */
    private void deleteCache() {
        // 删除缓存
        redisTemplate.delete(CacheConstants.NURSING_LEVEL_ALL_KEY);
    }
}
